from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
from datetime import datetime

class ChatSessionBase(BaseModel):
    """聊天会话基础模式"""
    title: Optional[str] = Field(None, max_length=200, description="会话标题")
    status: Optional[str] = Field("active", description="会话状态")

class ChatSessionCreate(ChatSessionBase):
    """创建聊天会话模式"""
    pass

class ChatSessionUpdate(BaseModel):
    """更新聊天会话模式"""
    title: Optional[str] = Field(None, max_length=200, description="会话标题")
    status: Optional[str] = Field(None, description="会话状态")

class ChatSession(ChatSessionBase):
    """聊天会话响应模式"""
    id: int
    session_id: str
    dify_conversation_id: Optional[str] = None
    user_id: int
    message_count: int = 0
    total_tokens: int = 0
    satisfaction_score: Optional[float] = None
    meta_data: Optional[Dict[str, Any]] = None
    tags: Optional[List[str]] = None
    created_at: datetime
    updated_at: Optional[datetime]
    last_message_at: Optional[datetime]
    
    class Config:
        from_attributes = True

class ChatMessageBase(BaseModel):
    """聊天消息基础模式"""
    content: str = Field(..., description="消息内容")
    content_type: Optional[str] = Field("text", description="内容类型")

class ChatMessageCreate(ChatMessageBase):
    """创建聊天消息模式"""
    inputs: Optional[Dict[str, Any]] = Field(None, description="输入参数，包含errorinfo等字段")

class ChatMessage(ChatMessageBase):
    """聊天消息响应模式"""
    id: int
    message_id: str
    session_id: int
    role: str
    model: Optional[str] = None
    tokens: Optional[int] = None
    response_time: Optional[float] = None
    rating: Optional[int] = None
    feedback: Optional[str] = None
    meta_data: Optional[Dict[str, Any]] = None
    created_at: datetime
    
    class Config:
        from_attributes = True

class ChatStats(BaseModel):
    """聊天统计模式"""
    total_sessions: int
    active_sessions: int
    total_messages: int
    today_messages: int
    average_rating: float

class MessageRating(BaseModel):
    """消息评价模式"""
    rating: int = Field(..., ge=1, le=5, description="评分(1-5)")
    feedback: Optional[str] = Field(None, description="反馈内容")